package com.crm.jdy.controller.ceshi.shiro;//package com.crm.jdy.common.shiro;
//
//
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//
//import java.util.HashSet;
//import java.util.List;
//import java.util.Set;
//
////@Service
//public class MyShiroRealm extends AuthorizingRealm {
//
//    JdyUserService jdUserService;
//
//    public MyShiroRealm(JdyUserService jdUserService) {
//        this.jdUserService = jdUserService;
//    }
//
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        System.out.println("添加角色和权限");
//        //添加角色和权限
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        JdyUser user= (JdyUser) principalCollection.getPrimaryPrincipal();
//        System.out.println("66666666666666666666666666666666666666");
//        System.out.println(user);
//        //保存所有角色名
////        List<UserPermission> jdyPermissions =  jdUserService.seleAllUserPriment(Integer.parseInt(user.getUsername()));
//        List<JdyUserDto> jdyUserRole =  jdUserService.seleAllUserUserRole(user.getUsername());
//
//        Set<String> allRoles = new HashSet<>();
//        //保存所有权限名
//        Set<String> allPermissions = new HashSet<>();
//        //查询对应角色
////        List<JdUserDto> userRole = jdUserService.getUserRole(user);
////        List<JdRole> lists = userRole.get(0).getLists();
//        for (JdyRoleDto  jdyRoleDto: jdyUserRole.get(0).getRoleList()){
//            if(jdyRoleDto !=null){
//                //2.1添加角色
//                simpleAuthorizationInfo.addRole(jdyRoleDto.getRoleName());
//            }
//            for (UserPermission rolePermission:jdyRoleDto.getUserPermissions()) {
//                simpleAuthorizationInfo.addStringPermission(rolePermission.getPermission());
//            }
//        }
////        //添加角色
////        simpleAuthorizationInfo.addRoles(allRoles);
////        //获取所有的权限  allPermissions
////        simpleAuthorizationInfo.addStringPermissions(allPermissions);
//        return simpleAuthorizationInfo;
//    }
//
//    //用户认证
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        // 1、登录认证的方法需要先执行，需要用他来判断登录的用户信息是否合法
//        String tokens = (String) token.getPrincipal();    // 取得token
//        String password = new String((char[])token.getCredentials());   // 取得密码
//        JdyUser  jdyUser = new JdyUser();
//        System.out.println(token);
//        System.out.println("------------------");
//        System.out.println(tokens);
//        System.out.println(password);
//        System.out.println("------------------");
//        jdyUser.setPassword(password);
//        jdyUser.setPhone(tokens);
//        JdyUser  jdyUse =  jdUserService.seletelistOne(jdyUser);
//
//        //1. 根据accessToken，查询用户信息
////         JdyUser tokenEntity = jdUserService.getById(tokens);  // 从redis中获取用户信息
////        if (tokenEntity == null) {
////            throw new IncorrectCredentialsException("token失效，请重新登录");
////        }
//
//        //3. 调用数据库的方法, 从数据库中查询 username 对应的用户记录
////        JdyUser user = jdUserService.getById(tokenEntity.getUserId());
////        if (user == null) {
////            throw new UnknownAccountException("用户不存在!");
////        }
//        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(jdyUse, jdyUse.getPassword(),jdyUse.getPhone());
//        return info;
////        System.out.println("用户认证");
////        // 需要通过用户名取得用户的完整信息，利用业务层操作
////        JdyUser users = new JdyUser();
////        users.setUsername(username);
////        users.setPassword(password);
////        List<JdUserDto> userRole=null;
//
////        System.out.println("jdUserService.list()");
////        System.out.println(jdUserService.list());
////        System.out.println("------=================-----------------");
////        try {
////            userRole = jdUserService.getUserRole(users);
////
////            users.setRoleId(userRole.get(0).getRoleId());
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
////        if(userRole == null){
////            System.out.println("22222222000000000000000000");
////            throw new UnknownAccountException("该用户名称不存在！");
////        }else{
////            users.setId(userRole.get(0).getId());
//            // 进行密码的验证处理
//            // 将数据库中的密码与输入的密码进行比较，这样就可以确定当前用户是否可以正常登录
////            return new SimpleAuthenticationInfo(users, password,  getName());
////        }
////        return new SimpleAuthenticationInfo(users, password,  getName());
//    }
//
//
//}
